//
//  ViewController.m
//  UI_10_TableView
//
//  Created by dllo on 16/3/22.
//  Copyright © 2016年 XWH. All rights reserved.
//

#import "ViewController.h"
#import "AddsViewController.h"
#import "AddsTableViewCell.h"
#import "DetilViewController.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate,LinkmanDetilDelgate>

@property (nonatomic, retain) NSMutableArray *arrOfKeys;

@property (nonatomic, retain) NSMutableDictionary *dic;

@property (nonatomic, retain) AddsTableViewCell *cell;

@property (nonatomic, retain) UITableView *tableView;

@end

@implementation ViewController

-(void)dealloc{
    
    
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self navigation];
    [self creatTableView];
    [self handleData];
    
}

-(void)navigation{
     self.navigationItem.title = @"联系人";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftButton:)];
    
}

-(void)leftButton:(UIBarButtonItem *)sender{
    AddsViewController *adds = [[AddsViewController alloc] init];
    
    adds.text = self;
    
    [self.navigationController pushViewController:adds animated:YES];
    
}

-(void)creatTableView{
   self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView reloadData];
    
    [self.view addSubview:self.tableView];
    [self.tableView release];

    
}

-(void)handleData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160101" ofType:@"plist"];
    self.dic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    
    self.arrOfKeys = [[self.dic allKeys] sortedArrayUsingSelector:@selector(compare:)].mutableCopy;
    
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arrOfKeys.count;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSString *key = [self.arrOfKeys objectAtIndex:section];
    NSArray *arr = [self.dic objectForKey:key];
    return arr.count;
    
   }


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"AddsTableViewCellIdentifier"];
    
    if (self.cell == nil) {
        self.cell = [[AddsTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AddsTableViewCellIdentifier"];
    }
    
    NSString *key = [self.arrOfKeys objectAtIndex:indexPath.section];
    
    NSArray *arr = [self.dic objectForKey:key];
    
    NSDictionary *dictionary = [arr objectAtIndex:indexPath.row];
    
   self.cell.textLabel.text = [dictionary valueForKey:@"name"];
    self.cell.detailTextLabel.text = [dictionary valueForKey:@"phoneNumber"];
    
    return self.cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetilViewController *detil = [[DetilViewController alloc] init];
    
    NSString *key = [self.arrOfKeys objectAtIndex:indexPath.section];
    
    NSArray *arr = [self.dic objectForKey:key];
    
    detil.dic = [arr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:detil animated:YES];
    
    
}

-(NSArray <NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.arrOfKeys;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.arrOfKeys objectAtIndex:section];
}

-(void)detil:(NSString *)text :(NSString *)name{
    
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:0];
    
    [dictionary setValue:name forKey:@"name"];
    [dictionary setValue:text forKey:@"phoneNumber"];
    
    NSString *strOfFirst = [[name substringToIndex:1] uppercaseString];
    
    NSMutableArray *mutArr = [self.dic objectForKey:strOfFirst];
    

    
    if (mutArr == nil) {
        mutArr = [NSMutableArray array];
        [mutArr addObject:dictionary];
        [self.dic setValue:mutArr forKey:strOfFirst];
        
        
    }else{
        [mutArr addObject:dictionary];

    }
    
    [self.arrOfKeys addObject:strOfFirst];
    self.arrOfKeys = [[self.dic allKeys] sortedArrayUsingSelector:@selector(compare:)].mutableCopy;
    
    
    [self.tableView reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
